define(['app','datetime','pagination','editor','exporting','autocomplete','filesaver', 'fileupload', 'js/service/agupload','js/filter/tree2Arr'], function( app ) {
	'use strict'; 
	//奖罚
	app.controller('Jiangcheng.comm', function($rootScope, $scope, dataFactoryCRM, $q,$http, AgUpload,$timeout) {
		
		$scope.sectors = {};
		$scope.psearch     = {};
		$rootScope.strhrtemp = null;
		//分页
		$scope.vPage  = {pageAll: 1,pageNow: 1,pageSize: 15,k: 3};	
		$scope.psearch['p'] = $scope.vPage.pageNow;
		$scope.psearch['psize'] = $scope.vPage.pageSize;
		//惩罚缘由
		$scope.cfcause = [{ k: '-1', v: '全部' }, { k: '0', v: '销售精英' }, { k: '1', v: '全勤奖' }, { k: '2', v: '月冠金奖' }];

		//惩罚方式
		$scope.cfways = [{ k: '-1', v: '全部' }, { k: '0', v: '现金' }, { k: '1', v: '伙食补助' }, { k: '2', v: '房补' }];
        /**获取部门信息 */
        $scope.getSector = function() {
            var _url = 'Fine/index';
            dataFactoryCRM.getDataByAPI(_url).then(function(_d) {
                if (_d.rcode) {
                    $scope.sectors = _d.data;
                }
            });
        };

		//首页
		$scope.getDataList  = function() {
			var _url ='Jiangcheng/getHrCommendData?'+$.param($scope.psearch);
			dataFactoryCRM.getDataByAPI(_url).then(function(data){
                if (data.rcode) {
                    $scope.sdata_list = data.data;
                    $scope.vPage.pageSize = data.psize;
                    $scope.vPage.pageAll = data.pall;
                    $scope.vPage.pageNow = data.p;
                } else {
                    $scope.vPage.pageAll = 0;
                }
			})
    	}
		//初始化页面显示数据（如果是新弹出独立窗口则从父窗口获取）
	    window.loadClientCacheToScope($scope, function(){
	    	$scope.getDataList();
	    });

	    //列表搜索
	    $scope.search = function(){
	    	$scope.getDataList();
	    }

        //初始化用户数据
		$scope.userlist  = function( skey ) {
			var _url ='Jiangcheng/getUserList?key=' + skey;
			dataFactoryCRM.getDataByAPI(_url).then(function(data){
                if (data.rcode) {
                    $scope.uslist = data.data;
                    var tval = $('#addinfo_user_id').val();
                    if ( tval != null ) {
                    	if ($rootScope.strhrtemp != null) {
                			$rootScope.strhrtemp += ',' + tval;
                    	}else{
                    		$rootScope.strhrtemp = tval;
                    	}
                    }
                }
			})
    	}
        //监听select事件
        $('#addfrm').bind('input propertychange', function() { 
            var vtext = $("#addfrm .sizer").text();
            if ( vtext != null && vtext.length > 0) {
            	$scope.userlist(vtext);
            }
        });

		//添加
		$scope.addShow         = function(){
			$timeout(function(){
				$("#addfrm").form('reset');			
			})
			$('.ui.addfrm').modal({
				autofocus:false,
	         	observeChanges: true,onApprove: function() {
	         		if ( $("#addfrm").form('is valid') ) {
	         			var para = null;
	         			if ($rootScope.strhrtemp != null) {
	         				para = $rootScope.strhrtemp;
	         			}
	         			var ntval = $('#addinfo_user_id').val();
	         			if ( ntval != null) {
	         				if ($.isArray( ntval )) {
								ntval = ntval.join(',');
	         				}
	         				if (para != null) {
	         					para += ',' +ntval;
	         				}else{
	         					para =  ntval;
	         				}
	         			}
	         			$scope.addinfo.user_id = para;
	         			$scope.addExecute($scope.addinfo);
	         		}else{
	         			return false;
	         		}
	          	}
	        }).modal('show');
		}

		//执行添加
		$scope.addExecute   = function( _d ){
			var _url ='Jiangcheng/addComm';
			dataFactoryCRM.postDataByAPI( _url , $.param( _d )).then(function(data){
				if(data.rcode == 1){
					$('#successMsg').html(data.msg);
					$('.ui.modalSuccess').modal('show');
					setTimeout(function() {
		                $('.ui.modalSuccess').modal('hide')
		            }, 1000);
		            $scope.getDataList();
				}else{
					$('#failMsg').html(data.msg);
					$('.ui.modalFail').modal('show');
					setTimeout(function() {
		                $('.ui.modalFail').modal('hide')
		            }, 2000);
				}	            
			})
			$rootScope.strhrtemp = null;
		}

		//删除
		$scope.pdelete = function( id ){
			$('#comfirmMsg').html('确定要删除记录吗？');
			$('.ui.modalComfirm').modal({
				observeChanges: true,onApprove: function() {
					var _url ='Jiangcheng/delComm';
					dataFactoryCRM.postDataByAPI( _url , 'id=' + id ).then(function(data){
						if(data.rcode == 1){
							$('#successMsg').html(data.msg);
							$('.ui.modalSuccess').modal('show');
							setTimeout(function() {
				                $('.ui.modalSuccess').modal('hide')
				            }, 1000);
				            $scope.getDataList();
						}else{
							$('#failMsg').html(data.msg);
							$('.ui.modalFail').modal('show');
							setTimeout(function() {
				                $('.ui.modalFail').modal('hide')
				            }, 1000);
						}	            
					})
				}
			}).modal('show');
		}

		//编辑
		$scope.editDetail     = function( cid ){
	        dataFactoryCRM.getDataByAPI('Jiangcheng/editorCommInfoById?id=' + cid ).then(function(_d){
	        	$timeout(function(){
		        	$scope.editinfo = _d.data;
					$('#editinfo_comm_type').dropdown('set selected', _d.data.comm_type);
					$('#editinfo_comm_class').dropdown('set selected', _d.data.comm_class);
	        	})
 	        })

			$('.ui.editfrm').modal({
				autofocus:false,
				onApprove: function() {
					if( $('#editfrm').form('is valid') ){
						$scope.editExecute( $scope.editinfo );
					}else{
					     return false;
					}
				}
	        }).modal('show');
		}

		$scope.editExecute = function( _d ){
			var _url ='Jiangcheng/editComm';
			dataFactoryCRM.postDataByAPI( _url , $.param( _d  ) ).then(function(data){
				if(data.rcode == 1){
					$('#successMsg').html(data.msg);
					$('.ui.modalSuccess').modal('show');
					setTimeout(function() {
		                $('.ui.modalSuccess').modal('hide')
		            }, 1000);
		            $scope.getDataList();
				}else{
					$('#failMsg').html(data.msg);
					$('.ui.modalFail').modal('show');
					setTimeout(function() {
		                $('.ui.modalFail').modal('hide')
		            }, 2000);
				}	            
			})
		}


        /**自动运行 */
        $scope.auto = function() {
            $scope.getSector();

        };
        $scope.auto();

	})
})